package com.itqf.ipmsys.exceptionhandler;


import com.itqf.ipmsys.utils.ResponseUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author corn
 * @create 2021-08-17 9:17
 * @Description: Shiro认证和授权失败处理器
 */
@CrossOrigin("*")
@ControllerAdvice
public class ShiroExceptionHandler {

    /**
     * 未认证异常捕捉
     * @param e 异常对象
     * @return 501的响应数据
     */
    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public Object unAuthentication(AuthenticationException e){
        return ResponseUtil.unlogin();
    }

    /**
     * 未授权异常捕捉
     * @param e 异常对象
     * @return 506的响应数据
     */
    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public Object unAuthorization(AuthorizationException e){
        return ResponseUtil.unauthz();
    }
}
